package dmt16.TelemetrySuit.UI
{
	import dmt16.TelemetrySuit.UI.Tiles.TileBase;
	import dmt16.TelemetrySuit.UI.Tiles.TileEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class TileContainer extends Sprite
	{
		public function TileContainer()
		{
			_tiles=new Vector.<TileBase>();
		}
		
		public function addTile(tile:TileBase):void{
			_tiles.push(tile);
			addChild(tile);
			redraw();
		}
		
		private var _tiles:Vector.<TileBase>;
		public function get tiles():Vector.<TileBase>{
			return _tiles;
		}
		
		private var _width:Number=500;
		public override function get width():Number{
			return _width;
		}
		public override function set width(value:Number):void{
			_width=value;
			redraw();
		}
		
		private var _height:Number=350;
		public override function get height():Number{
			return _height;
		}
		public override function set height(value:Number):void{
			_height=value;
			redraw();
		}
		
		private static const MIN_SPACE:Number=1;
		
		public function redraw():void{
			graphics.beginFill(0xFFCC33);
			var x_tiles:Number=Math.floor((_width-2*MIN_SPACE)/(TileBase.WIDTH+MIN_SPACE));
			var x_space:Number=(_width-(x_tiles*TileBase.WIDTH))/(x_tiles+1);
			for(var i:Number=0;i<_tiles.length;i++){
				_tiles[i].x=x_space+(i%x_tiles)*(TileBase.WIDTH+x_space);
				_tiles[i].y=x_space+Math.floor(i/x_tiles)*(TileBase.HEIGHT+x_space);
			}
		}
	}
}